﻿using System.Net;
using Newtonsoft.Json;
using RestSharp;
using xClient.Framework.StaticResource;
using xClient.Framework;
using System;

namespace cMES1009.Services
{
   public class ServiceMethod
    {
        public static RestClient _restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);

        public static ExecutionResult<dynamic> GetKPNCodeList(string vCode)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/kpncode/getlist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vCode))
                {
                    _request.AddQueryParameter("code", vCode);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取Code信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> GetGroupNameList(string vSection = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/group/getnamelist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);               
                if (!string.IsNullOrEmpty(vSection))
                {
                    _request.AddQueryParameter("section", vSection);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取工站信息异常." + ex.Message;
            }

            return result;
        }
        public static ExecutionResult<dynamic> GetGroupKPNList(string vGroup=null,string vKPNCode = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/groupkpn/getlist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vGroup))
                {
                    _request.AddQueryParameter("group", vGroup);
                }               
                if(!string.IsNullOrEmpty(vKPNCode))
                {
                    _request.AddQueryParameter("kpn", vKPNCode);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取工站捡料信息异常." + ex.Message;
            }

            return result;
        }
        public static ExecutionResult<dynamic> AddGroupKPNInfo(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/groupkpn/add";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "新增线别信息异常." + ex.Message;
            }

            return result;
        }
        public static ExecutionResult<dynamic> UpdateGroupKPNInfo(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/groupkpn/update";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "更新线别信息异常." + ex.Message;
            }

            return result;
        }
        public static ExecutionResult<dynamic> DeleteGroupKPNInfo(string vId)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = $"/basicdata//groupkpn/delete/{vId}";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);                

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "删除线别信息异常." + ex.Message;
            }

            return result;
        }
    }
}
